﻿//
// Created by Administrator on 2022/10/25.
// 荷兰国旗, 分成小于等于区和大于区,以给定的值(这里是数组的第一个值)为界限
//
#include <iostream>

using namespace std;

void swap(int &a, int &b)
{
    int t = a;
    a = b;
    b = t;
}

void printArr(int *arrP, int n)
{
    for (int i = 0; i < n; ++i) printf("%d ", *arrP++);
    printf("\n");
}

void le(int *a, int len, int &l, int &b)
{
    l = -1;
    b = len;
    int value = *a;
    int i = 0;
    while (i<b && l < b)
    {
        if (a[i] < value)
        {
            l++;
            swap(a[l], a[i]);
            i++;
        } else if (a[i] > value)
        {
            b--;
            swap(a[b], a[i]);
        } else
        {
           i++;
        }
    }
}


int main()
{
    int arr[] = {8, 8, 7, 6, 9, 4,3,2,9,5};
    int len = sizeof(arr) / sizeof(int);
    int less, big;
    le(arr, len, less, big);
    printf("less: %d, big:%d\n", less, big);
    printArr(arr, len);
}

